package com.eightgroups.config.cos;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.sign.Credentials;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Author: zzc
 * Email: 805046440@qq.com
 * Date: 2017/9/25
 * Time: 13:01
 * Describe: 腾讯云对象存储配置 https://console.qcloud.com/cos
 */
@Configuration
@EnableConfigurationProperties(CosConfigProperties.class)
@ConditionalOnProperty(name = "qcloud.enable")
public class CosConfig {

    @Autowired
    private CosConfigProperties cosConfigProperties;

    @Bean
    public COSClient cosClient() {
        // 初始化客户端配置
        ClientConfig clientConfig = new ClientConfig();
        // 设置bucket所在的区域，比如广州(gz), 天津(tj)
        clientConfig.setRegion(cosConfigProperties.getRegion());
        // 初始化秘钥信息
        Credentials cred = new Credentials(cosConfigProperties.getAppId(), cosConfigProperties.getSecretId(), cosConfigProperties.getSecretKey());
        // 初始化cosClient
        COSClient cosClient = new COSClient(clientConfig, cred);
        return cosClient;
    }

}
